package com.ruoyi.web.controller.business.customer.factory;

import com.ruoyi.business.core.domain.DivisionsCode;
import com.ruoyi.business.core.service.DivisionsCodeService;
import com.ruoyi.business.customer.domain.JbCustomer;
import com.ruoyi.business.customer.service.JbCustomerService;
import com.ruoyi.web.core.JoController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务 客户 工厂
 *
 * @author Jyl
 */
@Component
public class JbCustormerFactory extends JoController
{
    @Autowired
    private JbCustomerService jbCustomerService;
    @Autowired
    private DivisionsCodeService service;

    /**
     * 更新 收货地址
     *
     * @return
     */
    @Transactional
    public boolean updateAddress(JbCustomer jbCustomer, String address, String addressCodeAll)
    {
        //1获取地市编码
        String[] code = addressCodeAll.split(",");
        List<String> name = new ArrayList<>();
        for (String s : code)
        {
            DivisionsCode divisionsCode = service.findByCode(s);
            name.add(divisionsCode.getName());
        }

        //2更新收货地址
        jbCustomer.setDetailedAddress(address);

        if (code.length > 0)
        {
            jbCustomer.setLocalProvince(code[0]);
            jbCustomer.setLocalProvinceName(name.get(0));
        }

        if (code.length > 1)
        {
            jbCustomer.setLocalCity(code[1]);
            jbCustomer.setLocalCityName(name.get(1));
        }

        if (code.length > 2)
        {
            jbCustomer.setLocalCounty(code[2]);
            jbCustomer.setLocalCountyName(name.get(2));
        }

        if (code.length > 3)
        {
            jbCustomer.setLocalArea(code[3]);
            jbCustomer.setLocalAreaName(name.get(3));
        }

        jbCustomerService.updateById(jbCustomer);

        return true;
    }

}
